/*
 * Copyright 2018 the original author or authors.
 *
 * you can use the file or import project.but Do not take legal responsibility. Only for learning
 * communication.
 *
 * Java compiled version 1.8 or higher
 *
 *
 *
 *
 */

package org.liang.javasource.factory;

import java.util.List;
import org.liang.javasource.bean.BeanAttrsInfo;
import org.liang.javasource.bean.BeanInfo;
import org.liang.javasource.config.ClassConfig;
import org.liang.javasource.excel.CreateExampleExcel;

/**
 * 生成javasource源文件的工厂实现类
 * 
 * @author houliang
 * @date 2018年3月5日
 */
public class CreateJavaCodeOracleFactory extends CreateFactory {

	@Override
	public void createJavaCode() {
		ClassConfig config = super.getConfig();
		// 1、生成Bean对象
		List<BeanInfo> beanInfos = CreateExampleExcel.readExcelData(config);
		if (beanInfos == null) {
			return;
		}
		if (beanInfos.size() == 0) {
			System.err.println("可生成文件对象数为0");
			return;
		}

		// 2、bean对象属性转换
		for (int i = 0; i < beanInfos.size(); i++) {
			BeanInfo beanInfo = beanInfos.get(i);
			List<BeanAttrsInfo> attrsList = beanInfo.getAttrList();
			for (int j = 0; j < attrsList.size(); j++) {
				BeanAttrsInfo attr = attrsList.get(j);
				String name = attr.getName();
				if (name == null) {
					continue;
				}
				name = trans(name);
				attr.setName(name);
			}
		}

		// 2、生成java文件
		BeanFactory.generateBeans(beanInfos);
	}

	@Override
	public void printJavaCode() {
		ClassConfig config = super.getConfig();
		// 1、生成Bean对象
		List<BeanInfo> beanInfos = CreateExampleExcel.readExcelData(config);
		if (beanInfos == null) {
			return;
		}
		if (beanInfos.size() == 0) {
			System.err.println("可生成文件对象数为0");
			return;
		}
		// 2、生成java文件
		BeanFactory.printBeans(beanInfos);
	}

	private String trans(String name) {
		StringBuffer sb = new StringBuffer();

		name = name.toLowerCase();// 转换小写
		String[] ns = name.split("_");
		sb.append(ns[0]);

		for (int i = 1; i < ns.length; i++) {
			String t = ns[i];
			t = t.substring(0, 1).toUpperCase() + t.substring(1);
			sb.append(t);
		}

		return sb.toString();
	}
}
